<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 100px;
        height: 100px;
        background-color: rgb(198, 63, 76);
      }
    </style>
  </head>
  <body>
    <button>开始</button>
    <div></div>
    <script>
      window.onload = function () {
        var btn = document.querySelector("button");
        var div = document.querySelector("div");
        var begin = 0,
          target = 500,
          timer;
        btn.addEventListener("click", function () {
          move(timer, begin, target, 0.3, 100);
        });

        function move(timer, start, end, ratio, interval) {
          clearInterval(timer);
          timer = setInterval(function () {
            start += (end - start) * 0.3;
            console.log("🚀 ~ start:", start);

            if (Math.round(start) >= end) {
              // start = end;
              clearInterval(timer);
            }
            div.style.marginLeft = start + "px";
          }, 70);
        }
      };
    </script>
  </body>
</html>
